package com.htu.hdfs;
import java.io.IOException;  
import java.net.URI;  
import java.net.URISyntaxException;  
import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.fs.BlockLocation;  
import org.apache.hadoop.fs.FileStatus;  
import org.apache.hadoop.fs.FileSystem;  
import org.apache.hadoop.fs.Path;  
public class LocateFile {
	public static void main(String[] args) throws IOException, URISyntaxException {  
        Configuration conf = new Configuration();  
        String hdfsPath = "hdfs://localhost:9000";  
        FileSystem hdfs = FileSystem.get(new URI(hdfsPath), conf);  
  
        Path file = new Path("/hdfstest/sample_data");  
        FileStatus fileStatus = hdfs.getFileStatus(file);  
  
        BlockLocation[] location = hdfs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());  
        for (BlockLocation block : location) {  
            String[] hosts = block.getHosts();  
            for (String host : hosts) {  
                System.out.println("block:" +block + " host:"+ host);  
            }  
        }  
    }  
}
